/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * manage_order.java
 *
 * Created on Jul 25, 2011, 2:10:08 AM
 */
package introse1;
import java.util.ArrayList;
import java.sql.*;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;
import javax.swing.table.*;
import javax.swing.table.TableModel.*;
import javax.swing.JTable.*;
import java.util.Date;
/**
 *
 * @author Trisha
 */
public class manage_order extends javax.swing.JFrame {

    /** Creates new form manage_order */
    public manage_order(managementmodule managementmodule) {
        initComponents();
        DBConnect conn = new DBConnect();
        Connection konekta = conn.connect();
        PreparedStatement pstmt;
        ResultSet res;

        DefaultTableModel tmodel;
        tmodel=(DefaultTableModel)OrderTable.getModel();
        tmodel.setRowCount(0);

        try{
            pstmt = konekta.prepareStatement("Select * From order_form WHERE status = 1");
            res = pstmt.executeQuery();
            while(res.next()){
                String[] s1 = {res.getString("order_id"), res.getString("date"), res.getString("tableNum"),
                res.getString("TotalPrice")};
                tmodel.addRow(s1);
            }
        }catch(Exception e){
        }
        this.managementmodule = managementmodule;
        managementmodule.setVisible(false);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        OrderTable = new javax.swing.JTable();
        AddOrderButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        CancelOrderButton = new javax.swing.JButton();
        BackButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        ComputeSales = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        PaidOrderButton = new javax.swing.JButton();
        ErrorHandle = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Rockwell", 0, 24));
        jLabel1.setText("Manage Order");

        OrderTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Order ID", "Date", "Table Number", "Total Price"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(OrderTable);
        OrderTable.getColumnModel().getColumn(0).setResizable(false);
        OrderTable.getColumnModel().getColumn(1).setResizable(false);
        OrderTable.getColumnModel().getColumn(2).setResizable(false);
        OrderTable.getColumnModel().getColumn(3).setResizable(false);

        AddOrderButton.setFont(new java.awt.Font("Rockwell", 0, 18));
        AddOrderButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/addorder.png"))); // NOI18N
        AddOrderButton.setBorder(null);
        AddOrderButton.setBorderPainted(false);
        AddOrderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddOrderButtonActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel3.setText("Status:");

        CancelOrderButton.setFont(new java.awt.Font("Rockwell", 0, 18));
        CancelOrderButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/cancelorder.png"))); // NOI18N
        CancelOrderButton.setBorder(null);
        CancelOrderButton.setBorderPainted(false);
        CancelOrderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelOrderButtonActionPerformed(evt);
            }
        });

        BackButton.setFont(new java.awt.Font("Rockwell", 0, 18));
        BackButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/back.png"))); // NOI18N
        BackButton.setBorder(null);
        BackButton.setBorderPainted(false);
        BackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackButtonActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel4.setText("Add an Order:");

        jLabel5.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel5.setText("Compute Sales:");

        ComputeSales.setFont(new java.awt.Font("Rockwell", 0, 18));
        ComputeSales.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/computesales.png"))); // NOI18N
        ComputeSales.setBorder(null);
        ComputeSales.setBorderPainted(false);
        ComputeSales.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ComputeSalesActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel7.setText("Search:");

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/Haj Logo.JPG"))); // NOI18N

        PaidOrderButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/paid.png"))); // NOI18N
        PaidOrderButton.setBorder(null);
        PaidOrderButton.setBorderPainted(false);
        PaidOrderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PaidOrderButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(PaidOrderButton)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(BackButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel4)
                                        .addComponent(jLabel5)
                                        .addComponent(ComputeSales, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(AddOrderButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addComponent(CancelOrderButton, javax.swing.GroupLayout.Alignment.LEADING))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jTextField1))
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(46, 46, 46)
                                .addComponent(ErrorHandle, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jSeparator1)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(105, 105, 105)
                        .addComponent(jLabel6)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(ErrorHandle))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel7)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(AddOrderButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(CancelOrderButton)
                        .addGap(22, 22, 22)
                        .addComponent(PaidOrderButton)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ComputeSales)
                        .addGap(18, 18, 18)
                        .addComponent(BackButton))
                    .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(60, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackButtonActionPerformed
        this.setVisible(false);
        managementmodule.setVisible(true);
    }//GEN-LAST:event_BackButtonActionPerformed

    private void AddOrderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddOrderButtonActionPerformed
        this.setVisible(false);
        add_order next = new add_order(this);
        next.setVisible(true);
    }//GEN-LAST:event_AddOrderButtonActionPerformed

    private void ComputeSalesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ComputeSalesActionPerformed
        this.setVisible(false);
        compute_sales next = new compute_sales(this);
        next.setVisible(true);
    }//GEN-LAST:event_ComputeSalesActionPerformed

    private void PaidOrderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PaidOrderButtonActionPerformed
        DBConnect con = new DBConnect();
        Connection password = con.connect();
        PreparedStatement pstmt;
        ResultSet res;
        DefaultTableModel tmodel = (DefaultTableModel)OrderTable.getModel();
        try{
            int x = OrderTable.getSelectedRow();
            String str21 = (String)OrderTable.getModel().getValueAt(OrderTable.getSelectedRow(), 0);
            int y = Integer.parseInt(str21);
            try{
                pstmt = password.prepareStatement("Update order_form set status = ? Where order_id = ?");
                pstmt.setInt(1,0);
                pstmt.setInt(2, y);
                ErrorHandle.setText("Paid Order Success");
            }catch(Exception e){
                ErrorHandle.setText("Error in Database");
            }
            tmodel.removeRow(x);
        }catch(Exception e){
            ErrorHandle.setText("Select Something on The table");
        }
    }//GEN-LAST:event_PaidOrderButtonActionPerformed

    private void CancelOrderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelOrderButtonActionPerformed
        DBConnect con = new DBConnect();
        Connection password = con.connect();
        PreparedStatement pstmt;
        ResultSet res;
        DefaultTableModel tmodel = (DefaultTableModel)OrderTable.getModel();
        try{
            int x = OrderTable.getSelectedRow();
            String str21 = (String)OrderTable.getModel().getValueAt(OrderTable.getSelectedRow(), 0);
            int y = Integer.parseInt(str21);
            try{
                ResultSet set;
                ResultSet set2;
                int recipe;
                pstmt = password.prepareStatement("Update order_form set status = ? Where order_id = ?");
                pstmt.setInt(1,2);
                pstmt.setInt(2, y);
                pstmt.executeUpdate();

                pstmt = password.prepareStatement("Select * From order_food where order_id =?");
                pstmt.setInt(1, y);
                res = pstmt.executeQuery();
                while(res.next()){
                    recipe = Integer.parseInt(res.getString("recipe_id"));
                    pstmt = password.prepareStatement("Select * from recipe_ingredient where recipe_id = ?");
                    pstmt.setInt(1, recipe);
                    set = pstmt.executeQuery();

                    float ing = 0;

                    float quant = 0;
                    while(set.next()){
                        quant = Float.parseFloat(set.getString("quantity"));
                        pstmt = password.prepareStatement("Select * from inventory where inventoryID = ?");
                        pstmt.setInt(1, Integer.parseInt(set.getString("inventory_id")));
                        set2 = pstmt.executeQuery();
                        if(set2.next()){
                             ing += Float.parseFloat(set2.getString("ingredientUsed"));
                        }
                        ing -= quant;

                        pstmt = password.prepareStatement("Update inventory set ingredientUsed=? where inventoryID=?");
                        pstmt.setFloat(1, ing);
                        pstmt.setInt(2, Integer.parseInt(set.getString("inventory_id")));
                        pstmt.executeUpdate();
                        ing = 0;
                        ErrorHandle.setText("Cancel Order Success");
                    }
                }
            }catch(Exception e){
                ErrorHandle.setText("Error in Database");
            }

            tmodel.removeRow(x);

        }catch(Exception e){
            ErrorHandle.setText("Select Something on The table");
        }
        
    }//GEN-LAST:event_CancelOrderButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new manage_order(managementmodule).setVisible(true);
            }
        });
    }
    public static int gOrderID;
    public static VariableList varlist = new VariableList();
    private static compute_sales compute_sales;
    private static add_order add_order;
    private static edit_order edit_order;
    private static managementmodule managementmodule;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddOrderButton;
    private javax.swing.JButton BackButton;
    private javax.swing.JButton CancelOrderButton;
    private javax.swing.JButton ComputeSales;
    private javax.swing.JLabel ErrorHandle;
    private javax.swing.JTable OrderTable;
    private javax.swing.JButton PaidOrderButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
